/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.dei.perla.rockfall.utils;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.dei.perla.sys.device.fpc.dspic.DSPicFPC;

public class PopulateDB {
	
	private static final int FPC_NUMBER = 4;
	private static final int ITERATION_NUMBER = 50;
	private static ArrayList<DSPicFPC> dspicList;
	private static final ExecutorService executor = Executors.newFixedThreadPool(FPC_NUMBER * 2);
	
	public static void main(String[] args) {
		System.out.println("Setting up...");
		
		DSPicDataGenerator gen = new DSPicDataGenerator();
		dspicList = new ArrayList<DSPicFPC>();
		
		DSPicFPC fpc;
		for (int i = 0; i < FPC_NUMBER; i ++) {
			fpc = new DSPicFPC(gen.getCommunicationPipes(), i);
			dspicList.add(fpc);
			executor.execute(new DatabaseDump(fpc.registerQuery(null)));
		}
		long begin = System.nanoTime();
		System.out.println("Generating data...");
		for (int i = 0; i < ITERATION_NUMBER; i++) {
			gen.putDataOnPipe();
			System.out.print("|");
		}
		long end = System.nanoTime();
		System.out.println("Stopping, just wait for the " + FPC_NUMBER + " DBDump to terminate!!");
		System.out.println("Took approx. " + (end - begin) + " nanoseconds to generate the data");
		gen.stop();
		executor.shutdown();
	}
}
